package cn.xinfei.xdecision.data.core.util;


import cn.xinfei.xdecision.data.core.vo.credit.CreditChannelRouteRequest;
import cn.xinfei.xdecision.data.core.vo.credit.CreditStatusRequest;
import cn.xinfei.xdecision.data.core.vo.credit.CreditStatusDTO;
import cn.xinfei.xdecision.model.CreditVarRequest;
import cn.xinfei.xdecision.model.VariableApiRequest;
import com.alibaba.fastjson.JSONObject;

import java.util.Objects;
import java.util.Set;


public class VariableConvertUtil {

    public static CreditStatusRequest fromCreditVarRequest(CreditVarRequest creditVarRequest) {
        CreditStatusRequest creditStatusRequest = new CreditStatusRequest();
        creditStatusRequest.setCreditChannel(creditVarRequest.getInputParams().getString("credit_channel"));
        creditStatusRequest.setIdCardNumber(creditVarRequest.getInputParams().getString("id_card_number"));
        creditStatusRequest.setMobile(creditVarRequest.getInputParams().getString("mobile"));
        creditStatusRequest.setName(creditVarRequest.getInputParams().getString("name"));
        creditStatusRequest.setStage((String)creditVarRequest.getInputParams().getOrDefault("stage","01"));
        return creditStatusRequest;

    }


    public static CreditStatusDTO toCreditStatusDTO(CreditStatusRequest creditStatusReq,
                                                    String requestId,
                                                    Set<String> varCodes,
                                                    Long applyId,
                                                    String app,
                                                    String innerApp) {
        CreditStatusDTO creditStatusDto = new CreditStatusDTO();
        if (Objects.nonNull(creditStatusReq)) {
            creditStatusDto.setCreditChannel(creditStatusReq.getCreditChannel());
            creditStatusDto.setIdCardNumber(creditStatusReq.getIdCardNumber());
            creditStatusDto.setMobile(creditStatusReq.getMobile());
            creditStatusDto.setName(creditStatusReq.getName());
            creditStatusDto.setStage(creditStatusReq.getStage());
        }
        creditStatusDto.setApplyId(applyId);
        creditStatusDto.setInnerApp(innerApp);
        creditStatusDto.setApp(app);
        creditStatusDto.setRequestId(requestId);
        creditStatusDto.setVarCodes(varCodes);
        return creditStatusDto;

    }

    public static CreditStatusDTO fromVariableApiRequest(VariableApiRequest apiRequest) {
        CreditStatusDTO creditStatusDto = new CreditStatusDTO();
        if (Objects.nonNull(apiRequest)) {
            JSONObject inputParams = apiRequest.getInputParams();
            String idCardNumber = inputParams.getString("id_card_number");
            String mobile = inputParams.getString("mobile");
            String name = inputParams.getString("name");
            String stage = inputParams.getString("stage");
            Long applyId = inputParams.getLong("apply_id");
            String innerApp = inputParams.getString("inner_app");
            String app = inputParams.getString("app");

            creditStatusDto.setIdCardNumber(idCardNumber);
            creditStatusDto.setMobile(mobile);
            creditStatusDto.setName(name);
            creditStatusDto.setStage(stage);
            creditStatusDto.setApplyId(applyId);
            creditStatusDto.setInnerApp(innerApp);
            creditStatusDto.setApp(app);
            creditStatusDto.setRequestId(apiRequest.getRequestId());
            creditStatusDto.setVarCodes(apiRequest.getVarCodes());
        }

        return creditStatusDto;
    }

    public static CreditStatusRequest fromCreditStatusDTO(CreditStatusDTO creditStatusDTO) {
        CreditStatusRequest creditStatusRequest = new CreditStatusRequest();
        if (Objects.nonNull(creditStatusDTO)) {
            creditStatusRequest.setCreditChannel(creditStatusDTO.getCreditChannel());
            creditStatusRequest.setMobile(creditStatusDTO.getMobile());
            creditStatusRequest.setName(creditStatusDTO.getName());
            creditStatusRequest.setStage(creditStatusDTO.getStage());
            creditStatusRequest.setIdCardNumber(creditStatusDTO.getIdCardNumber());
            creditStatusRequest.setIdCardProtyle(creditStatusDTO.getIdCardProtyle());
        }
        return creditStatusRequest;
    }

    public static CreditChannelRouteRequest toCreditRuteChannelDTO(CreditStatusDTO creditStatusDTO) {
        CreditChannelRouteRequest creditRuteChannelRequest = new CreditChannelRouteRequest();
        if (Objects.nonNull(creditStatusDTO)) {
            creditRuteChannelRequest.setLoanNum(creditStatusDTO.getLoanNum());
            creditRuteChannelRequest.setInnerApp(creditStatusDTO.getInnerApp());
            creditRuteChannelRequest.setStage(creditStatusDTO.getStage());
            creditRuteChannelRequest.setIdCardNumber(creditStatusDTO.getIdCardNumber());
        }
        return creditRuteChannelRequest;
    }

    public static CreditVarRequest toCreditVarRequest(VariableApiRequest variableApiRequest) {

        CreditVarRequest creditVarRequest = new CreditVarRequest();
        if(Objects.nonNull(variableApiRequest)) {
            creditVarRequest.setVarCodes(variableApiRequest.getVarCodes());
            creditVarRequest.setInputParams(variableApiRequest.getInputParams());
            creditVarRequest.setRequestId(variableApiRequest.getRequestId());
        }
        return creditVarRequest;
    }

    public static VariableApiRequest toVariableApiRequest(CreditStatusDTO creditStatusDTO) {
        VariableApiRequest variableApiRequest = new  VariableApiRequest();
        if (Objects.nonNull(creditStatusDTO)) {
            variableApiRequest.setVarCodes(creditStatusDTO.getVarCodes());
            variableApiRequest.setRequestId(creditStatusDTO.getRequestId());
            variableApiRequest.setInputParams(
                    new JSONObject()
                            .fluentPut("app",creditStatusDTO.getApp())
                            .fluentPut("id_card_number",creditStatusDTO.getIdCardNumber())
                            .fluentPut("mobile",creditStatusDTO.getMobile())
                            .fluentPut("apply_id",creditStatusDTO.getApplyId())
                            .fluentPut("stage",creditStatusDTO.getStage())
                            .fluentPut("name",creditStatusDTO.getName())
                            .fluentPut("credit_channel",creditStatusDTO.getCreditChannel())
                            .fluentPut("inner_app",creditStatusDTO.getInnerApp())
            );
        }
        return variableApiRequest;
    }

}
